<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="status" label="预约状态">
          <el-select v-model="queryParams.status" placeholder="请选择" @change="selectStatus">
            <el-option label="已预约" value="RESERVED"></el-option>
            <el-option label="已结束" value="ENDED"></el-option>
            <el-option label="已取消" value="CANCELED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="关联样本" prop="number"/>
        <el-table-column label="用户名" prop="userName"/>
        <el-table-column label="手机号" prop="phone"/>
        <el-table-column label="预约时间" prop="reservationDateText"/>
        <el-table-column label="预约状态" prop="statusText"/>
        <el-table-column label="提交时间" prop="createTimeText"/>
        <el-table-column fixed="right" label="操作"  min-width="280" width="280">
          <template #default="scope">
            <el-button type="warning" size="small" :disabled="scope.row.status != 'RESERVED'" link @click="endReservation(scope.row.id)"><i class="el-icon-edit"></i> 标记结束</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>

import reportReservationIndexApi from '@/api/opsHpc/aliyun/reportReservation'
import { formatDate } from '@/utils/formatTime'
export default {
	name: 'dictIndex',
	data() {
		return {
			queryParams: {
        status: '',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      statusMap: {
        RESERVED: '已预约',
        ENDED: '已结束',
        CANCELED: '已取消'
      },
		};
	},
  methods: {
    selectStatus () {
      this.handleQuery();
    },
    endReservation (id) {
      this.$confirm('请确认是否已经和用户沟通，并已经完成样本报告的解读?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        reportReservationIndexApi.end(id).then(res => {
          this.$message({ message: '报告解读预约已处理！', type: 'success' });
          this.handleQuery();
        })
      }).catch(() => {});
    },
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize,
        status: this.queryParams.status
      };
      reportReservationIndexApi.page(params).then((res) => {
        let data = res.data;
        let index = 1
        data.content.forEach((v) => {
          v.createTimeText = formatDate(new Date(v.createTime), "YYYY-mm-dd HH:MM:SS")
          v.updateTimeText = formatDate(new Date(v.updateTime), "YYYY-mm-dd HH:MM:SS")
          v.reservationDateText = formatDate(new Date(v.reservationDate), "YYYY-mm-dd HH:MM")
          v.statusText = this.statusMap[v.status];
          v.dataIndex = index++;
        });
        this.dataList = data.content;
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        keywords: '',
        pageNum: 0,
        pageSize: 10,
      };
      this.handleQuery();
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>
.el-pagination {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
</style>